package com.chinaisafe.user.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.chinaisafe.module.user.request.RoleUserReqVO;
import com.chinaisafe.module.user.request.UserRoleReqVO;
import com.chinaisafe.user.biz.SysUserRoleBiz;
import com.chinaisafe.user.entity.SysUserRole;
import com.chinaisafe.user.mapper.SysUserRoleMapper;
import com.chinaisafe.user.service.SysUserRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * SysUserRoleBizImpl
 * Description：
 *
 * @author tby
 * @date 2024/7/15 14:19
 */
@Service
public class SysUserRoleBizImpl implements SysUserRoleBiz {

    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void roleUpdateUser(RoleUserReqVO roleUserReq) {
        //1. 删除没有的人员
        sysUserRoleMapper.deleteByRole(roleUserReq.getRoleId());
        if (CollectionUtil.isEmpty(roleUserReq.getUserIds())){
            return;
        }
        List<SysUserRole> sysUserRoles = new ArrayList<>(roleUserReq.getUserIds().size());
        for (Long userId : roleUserReq.getUserIds()) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setSysUserId(userId);
            sysUserRole.setSysRoleId(roleUserReq.getRoleId());
            sysUserRoles.add(sysUserRole);
        }
        sysUserRoleService.saveBatch(sysUserRoles);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void userUpdateRole(UserRoleReqVO userRoleReq) {
        //1. 删除没有的角色
        //sysUserRoleMapper.deleteByUser(userRoleReq.getUserId());

        if (CollectionUtil.isEmpty(userRoleReq.getRoleIds())){
            return;
        }
        List<SysUserRole> sysUserRoles = new ArrayList<>(userRoleReq.getRoleIds().size());
        for (Long roleId : userRoleReq.getRoleIds()) {
            SysUserRole sysUserRole = new SysUserRole();
            //sysUserRole.setSysUserId(userRoleReq.getUserId());
            sysUserRole.setSysRoleId(roleId);
            sysUserRoles.add(sysUserRole);
        }
        sysUserRoleService.saveBatch(sysUserRoles);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUserAddRoles(UserRoleReqVO userRoleReq) {
        //删除已有的用户角色关系
        for (Long roleId : userRoleReq.getRoleIds()){
            sysUserRoleMapper.deleteByRoleUser(userRoleReq.getUserIds(),roleId);
        }
        List<SysUserRole> sysUserRoles = new ArrayList<>(userRoleReq.getRoleIds().size()*userRoleReq.getUserIds().size());
        for (Long roleId : userRoleReq.getRoleIds()) {
            for (Long userId : userRoleReq.getUserIds()) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setSysUserId(userId);
                sysUserRole.setSysRoleId(roleId);
                sysUserRoles.add(sysUserRole);
            }
        }
        sysUserRoleService.saveBatch(sysUserRoles);
    }
}
